<template>
  <el-dialog title="设置角色" :visible.sync="dialogShow" @close="closeDialog">
    <!--多选框组-->
    <el-checkbox-group v-model="checkList">
      <el-checkbox
        v-for="item in roleList"
        :key="item.id"
        :label="item.id"
        :disabled="item.id === 1"
      >{{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row type="flex" justify="center">
      <el-button @click="dialogShow = false">取 消</el-button>
      <el-button type="primary" @click="submitRole">确 定</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleListApi } from '@/api/setting'
import { assignRole, getEmployeesInfoApi } from '@/api/employees'

export default {
  name: 'SetRole',
  data() {
    return {
      dialogShow: false,
      roleList: [], // 角色列表
      checkList: [], // 选中的角色列表
      userId: null // 当前员工id
    }
  },
  methods: {
    // 获取角色列表
    async getAllRoleList() {
      const { rows } = await getRoleListApi({
        page: 1,
        pagesize: 1000
      })
      console.log(rows)
      this.roleList = rows
    },
    // 获取当前员工信息
    async getCurrentRole(id) {
      this.userId = id
      const res = await getEmployeesInfoApi(id)
      this.checkList = res.roleIds || []
    },
    // 提交角色
    async submitRole() {
      // 发送请求
      await assignRole({
        id: this.userId,
        roleIds: this.checkList
      })
      this.$message.success('关联成功')
      this.dialogShow = false
    },
    // 关闭弹窗事件
    closeDialog() {
      this.checkList = []
      this.userId = null
    }
  }
}
</script>

<style scoped>

</style>
